import validate from 'wechat-validate'

Page({
  behaviors: [validate],
  data: {
    description: '',
    appointment: '', // 预约日期，格式形如：YYYY-MM-dd
    mobile: '',
    houseId: '',
    houseName: '',
    repairItemId: '',
    repairItemName: '',
    currentDate: Date.now(),
    // currentDate: new Date().getTime(),
    // houseLayerVisible: false,
    repairLayerVisible: false,
    // dateLayerVisible: false,
    // houseList: [
    //   { name: '北京西三旗花园1号楼 101' },
    //   { name: '北京东村家园3号楼 302' },
    //   { name: '北京育新花园3号楼 703' },
    //   { name: '北京天通苑北苑8号楼 403' },
    // ],
    repairItem: [],
    // repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    attachment: [],
    // attachment: [
    //   { url: '/repair_pkg/static/uploads/attachment.jpg' },
    //   { url: '/repair_pkg/static/uploads/attachment.jpg' },
    // ],
  },

  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }],
  },

  // onSelectedHouse(e) {
  //   const { id, name } = e.detail
  //   this.setData({
  //     houseId: id,
  //     houseName: name,
  //   })
  // },

  delAttachment(e) {
    this.data.attachment.splice(e.detail.index, 1)
    this.setData({ attachment: this.data.attachment })
  },

  async uploadAttachment(e) {
    const tempFilePathList = e.detail.file.map((v) => v.tempFilePath)
    const promiseList = tempFilePathList.map((v) => wx.http.uploadFile(v, 'repair_attachment'))
    const res = await Promise.all(promiseList)
    const attachment = res.map((v) => v.data)
    this.setData({ attachment: [...this.data.attachment, ...attachment] })
  },

  // onConfirmDateTimePicker(e) {
  //   this.setData({
  //     appointment: wx.utils.formatDateToString(e.detail),
  //     dateLayerVisible: false,
  //   })
  // },

  onSelectRepairLayer(e) {
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name,
    })
  },

  // onSelectHouseLayer(e) {
  //   this.setData({
  //     houseId: e.detail.id,
  //     houseName: e.detail.name,
  //   })
  // },
  // async openHouseLayer() {
  //   const res = await wx.http({
  //     url: '/house',
  //   })

  //   if (!res.data.length) {
  //     return wx.utils.toast('您暂无审核通过的房屋！')
  //   }

  //   this.setData({ houseList: res.data, houseLayerVisible: true })
  // },
  // closeHouseLayer() {
  //   this.setData({ houseLayerVisible: false })
  // },
  async openRepairLayer() {
    // 如果已经有数据了，就没必要反复发请求，因为这个数据不会变得很频繁
    if (this.data.repairItem.length) {
      return this.setData({ repairLayerVisible: true })
    }

    const res = await wx.http({
      url: '/repairItem',
    })

    this.setData({ repairItem: res.data, repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  // openDateLayer() {
  //   this.setData({ dateLayerVisible: true })
  // },
  // closeDateLayer() {
  //   this.setData({ dateLayerVisible: false })
  // },
  async goList() {
    if (!this.validate()) return

    const { houseId, repairItemId, mobile, appointment, description, attachment } = this.data

    await wx.http({
      method: 'POST',
      url: '/repair',
      data: {
        houseId,
        repairItemId,
        mobile,
        appointment,
        description,
        attachment,
      },
    })

    let curPages = getCurrentPages()

    // 首页 => 过来的
    if (curPages.length === 2) {
      wx.redirectTo({
        url: '/repair_pkg/pages/list/index',
      })
    }
    // 从我的 => 报修列表 => 过来的
    else if (curPages.length === 3) {
      wx.navigateBack()
    }
    // 其他情况
    else {
      wx.reLaunch({
        url: '/repair_pkg/pages/list/index',
      })
    }
  },
})
